import yaml
import InfoTracer
import ReInfo
import Statistics
from selenium import webdriver
import schedule
import Email


def job():
    # 读取配置文件
    with open("config.yaml", "r", encoding="utf-8") as file:
        config = yaml.safe_load(file)

    # 使用配置
    weapon_dict = config["weapon_dict"]
    coordinates = config["coordinates"]
    preferences = config["account_preferences"]
    send_email = config["send_email"]


    driver = InfoTracer.init_driver()
    InfoTracer.input_default_info(
        driver=driver,
        preferences=preferences,
        weapon_dict=weapon_dict,
        coordinates=coordinates,
    )  # 按需筛选账号

    # 抓取并解析账号信息
    details_list = InfoTracer.extract_account_info(driver)
    price_list = InfoTracer.extract_account_price(driver)

    formatted_info = ReInfo.extract_account_info(details_list, price_list)

    # 追踪价格变化 并自动记录到account_data.yaml
    added, changed = Statistics.track_price_changes(formatted_info)

    print("新增账号编码：", added)
    print("价格变化账号编码：", changed)
    
    if send_email:
        mail_content = Statistics.price_change_log(changed_accounts=changed, write_to_logs=True)
        email = Email.Email(mail_content=mail_content)
        email.send_email()

    driver.quit()

job()  # 马上运行一次
# 设置定时任务，每隔x秒执行一次
schedule.every(40).seconds.do(job)

while True:
    # 运行任务
    schedule.run_pending()




